#include "UTF-8toGB2312.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <FS.h>
#define TFT_DC 3
#define TFT_CS 15
ESP8266WiFiMulti WiFiMulti;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
File f;
void drawchar(u16 chr,int x,int y){
if(chr>=0xa1a0){
u8 high,low;u8 buf[32];
high=chr>>8;
low=chr&0xff;

  f.seek(32*((high-0xa0-1)*94+(low-0xa0-1)),SeekSet);
  f.read(buf,32);
  for(int i=0;i<16;i++){
    u8 readr=buf[i*2];
    for(int j=0;j<8;j++) {if(readr&(0x80>>j)) tft.drawPixel(x+j,y+i,ILI9341_BLACK);}
    readr=buf[i*2+1];
    for(int j=0;j<8;j++) if(readr&(0x80>>j)) tft.drawPixel(x+j+8,y+i,ILI9341_BLACK);
  }
}
}
bool printchs(String str,int x,int y){
    int num=str.length();
int  n=num/3+10;
  u16 s[n+1];
 bool pd=false; 
 
  Utf8ToGb2312(str.c_str(),num,s);
 

 int j=0;
  for(int i=0;i<n;i++){
    if(s[i]==0xffff) break;
    if(x+i*16+16<241)
    drawchar(s[i],x+i*16,y);
    else
    {drawchar(s[i],j*16,y+16);j++;pd=true;}
  }
  return pd;
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
    tft.begin();
    SPIFFS.begin();
    tft.setRotation(2);
    tft.fillScreen(ILI9341_WHITE);
    tft.setTextColor(ILI9341_BLACK); tft.setTextSize(2);
   f = SPIFFS.open("/hzk16.bin", "r");
WiFiMulti.addAP("4125", "4064064066");
 // String str="的完整示例，也是出错警告，书写的有些早";
//printchs(str,50,50);
 Serial.println("begin");
}
  
void loop() {bool succ=false;
  // put your main code here, to run repeatedly:
    if((WiFiMulti.run() == WL_CONNECTED)) {
tft.fillScreen(ILI9341_WHITE);

        HTTPClient http;

        Serial.print("[HTTP] begin...\n");
        // configure traged server and url
        //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
        http.begin("http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx?DataSign=ODY5MWQxYTlhYTJhMTE2MDI5ZDdiOWZjZTI3ZmE1OGY%3d&DataType=2&EBusinessID=1268403&RequestData=%7b%22LogisticCode%22%3a%22+700286638602%22%2c%22ShipperCode%22%3a%22YTO%22%7d&RequestType=1002"); 
       // http.begin("http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx?DataSign=MGM4MDlkNzFjMDcwOTllYjY4MmY2ZTgxZjQ0M2YzNzg%3d&DataType=2&EBusinessID=1268403&RequestData=%7b%22LogisticCode%22%3a%22883012593559661943%22%2c%22ShipperCode%22%3a%22YTO%22%7d&RequestType=1002");
//http.addHeader("apikey","c6f48c4035d4186a9a422efbbefd218b");
        Serial.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            Serial.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {  
                String payload = http.getString();
                Serial.println(payload);
             DynamicJsonBuffer jsonBuffer;
                
                JsonObject& root = jsonBuffer.parseObject(payload);
                
   int xx=0,yy=16;  //Serial.println(sizeof(root["Traces"])); 
    
for(int i=100;i>=0;i--){
  String a=root["Traces"][i]["AcceptTime"];
String b=root["Traces"][i]["AcceptStation"];
if(a!=""){ 
Serial.println(a);
Serial.println(b);
 tft.setCursor(xx, yy);
 tft.println(a);
if(printchs(b,xx,yy+16)) yy=yy+48;else yy=yy+32;succ=true;
}
}



Serial.println();
Serial.println();
            }
        } else {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
  
tft.setCursor(0, 0);  
 if(succ) tft.print("success");else tft.print("fail");
    for(int i=59;i>=00;i--){tft.setCursor(200, 0);
    tft.fillRect(200,0,40,16,ILI9341_WHITE);
    tft.print(i);
    delay(1000);
    }
      }
 tft.setCursor(0, 0);  
 if(succ) tft.print("success");else tft.print("fail");   
}